﻿namespace Wisdom.Core.WcfExtensions
{
    /// <summary>
    /// 终结点名称生成器
    /// </summary>
    public class EndpointNameGenerator
    {
        private readonly string _prefix = string.Empty;
        private readonly string _separator = string.Empty;
        private readonly bool _isIncludeInterfacePrefix = true;

        /// <summary>
        /// 构造函数
        /// </summary>
        public EndpointNameGenerator()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prefix">终结点名称前缀</param>
        /// <param name="separator">终结点名称分隔符</param>
        public EndpointNameGenerator(string prefix, string separator)
            : this(prefix, separator, true)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="prefix">终结点名称前缀</param>
        /// <param name="separator">终结点名称分隔符</param>
        /// <param name="isIncludeInterfacePrefix">是否包含服务契约默认接口前缀“I”</param>
        public EndpointNameGenerator(string prefix, string separator, bool isIncludeInterfacePrefix)
        {
            _prefix = prefix;
            _separator = separator;
            _isIncludeInterfacePrefix = isIncludeInterfacePrefix;
        }

        /// <summary>
        /// 获取传入服务契约对应的终结点名称
        /// </summary>
        /// <typeparam name="TService">服务契约类型</typeparam>
        /// <returns>返回服务契约对应的终结点名称</returns>
        public string GetEndPointName<TService>()
        {
            var serviceType = typeof(TService);
            var serverName = _isIncludeInterfacePrefix ? serviceType.Name : serviceType.Name.Substring(1);
            return string.Format(_prefix + _separator + "{0}", serverName);
        }
    }
}